package ai.people.netmon.twitter.monitor.service.impl;

import ai.people.core.standard.analysis.entity.request.JoinAnalysisRequest;
import ai.people.core.standard.analysis.service.AnalysisDataUserService;
import ai.people.core.standard.gather.userconfig.UserConfigSyncService;
import ai.people.netmon.framework.businessenum.AnalysisSourceEnum;
import ai.people.netmon.framework.businessenum.AnalysisTypeEnum;
import ai.people.netmon.framework.constant.SourceCollectionConstant;
import ai.people.netmon.framework.domain.common.monitor.MonitorUsersListVO;
import ai.people.netmon.framework.domain.common.monitor.MonitorUsersVO;
import ai.people.netmon.framework.domain.gather.config.GatherSyncExtended;
import ai.people.core.standard.gather.entity.UserConfigAccountData;
import ai.people.netmon.framework.domain.telegram.entity.AnalysisDataUser;
import ai.people.netmon.framework.domain.twitter.monitor.entity.TwitterMonitorUsers;
import ai.people.netmon.framework.domain.twitter.monitor.request.MonitorUsersRequest;
import ai.people.netmon.framework.domain.twitter.view.entity.TwitterAccountView;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.response.PageResponse;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.netmon.twitter.monitor.mapper.MonitorUsersMapper;
import ai.people.netmon.twitter.monitor.service.TwitterMonitorUsersService;
import ai.people.netmon.twitter.view.mapper.TwitterAccountViewMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhenglin
 * @since 2022-03-08
 */
@Service
@Slf4j
public class TwitterMonitorUsersServiceImpl extends ServiceImpl<MonitorUsersMapper, TwitterMonitorUsers> implements TwitterMonitorUsersService, UserConfigSyncService {

    @Value("${data-sync.account-url}")
    private String accountSyncUrl;
    @Autowired
    private AnalysisDataUserService analysisDataUserService;
    @Autowired
    TwitterAccountViewMapper viewMapper;

    @Override
    public MonitorUsersVO getUsers(String userName) {
        AssertUtils.isTrue(StringUtils.isNotBlank(userName), CommonEnum.FAIL);
        LambdaQueryWrapper<TwitterAccountView> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TwitterAccountView::getAccountName, userName);
        TwitterAccountView one = viewMapper.selectOne(queryWrapper);
        if (one == null){
            MonitorUsersVO twitterMonitorUsersVO = new MonitorUsersVO();
            twitterMonitorUsersVO.setUsername(userName);
            return twitterMonitorUsersVO;
        }
        return new MonitorUsersVO(one);
    }

    @Override
    public PageResponse<MonitorUsersListVO> listUserInfo(MonitorUsersRequest request) {
        Page<TwitterAccountView> page = new Page<>(request.getOriginPageNo(), request.getPageSize());
        LambdaQueryWrapper<TwitterAccountView> queryWrapper = request.createQueryWrapper();
        Page<TwitterAccountView> result = viewMapper.selectPage(page, queryWrapper);
        List<TwitterAccountView> records = result.getRecords();
        List<MonitorUsersListVO> resultVo = new ArrayList<>();
        records.forEach(x -> resultVo.add(new MonitorUsersListVO(x)));
        return PageResponse.builder(resultVo, result.getTotal(), result.getPages(), result.getCurrent());
    }

    @Override
    public void addAnalysis(JoinAnalysisRequest request) {
        //先查库
        String analysisTargetName = request.getAnalysisTargetName();
        AssertUtils.isTrue(StringUtils.isNotBlank(analysisTargetName), CommonEnum.FAIL);
        LambdaQueryWrapper<TwitterMonitorUsers> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(TwitterMonitorUsers::getUserName, analysisTargetName);
        TwitterMonitorUsers monitorUsers = getOne(queryWrapper);
        AssertUtils.isNotNull(monitorUsers, CommonEnum.QUERY_NOT_FOUND);
        String userId = monitorUsers.getUserId();
        String userName = monitorUsers.getUserName();
        String source = AnalysisSourceEnum.twitter.name();
        //保存到账号分析中间表
        AnalysisDataUser analysisDataUser = new AnalysisDataUser(request.getUserId(), userId, userName, source, AnalysisTypeEnum.ACCOUNT.getCode());
        analysisDataUser.setAvatarBackground(monitorUsers.getBackgroundImage());
        analysisDataUser.setAvatarUrl(monitorUsers.getHeaderPicture());
        analysisDataUserService.addAnalysisData(analysisDataUser);
        //数据同步
        syncAccounts(Collections.singletonList(new UserConfigAccountData(userName,null, source, SourceCollectionConstant.COLLECTION_FREQUENCY, null)), source, accountSyncUrl, new GatherSyncExtended());
    }

    @Override
    public void updateCollectionStatus(List account) {

    }
}
